package com.lebang.service;

import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.AppDataCollect;
import com.lebang.mapper.AppDataCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DataCollectService {

    @Autowired
    private AppDataCollectMapper appDataCollectMapper;


    /**
     * 收集数据
     *
     * @param appId
     * @param appName
     * @param appPkg
     * @param targetUrl
     * @param json
     * @return
     */
    @DataSource("commonDB")
    public boolean collect(Integer appId, String appName, String appPkg, String targetUrl, String json) {

        AppDataCollect appDataCollect = appDataCollectMapper.selectOneByAppPkg(appPkg, appId);
        if (appDataCollect != null) {
            appDataCollect.setModifyTime(LocalDateTime.now());
            appDataCollect.setAppName(appName);
            appDataCollect.setAppPkg(appPkg);
            appDataCollect.setTargetUrl(targetUrl);
            appDataCollect.setJson(json);
            appDataCollect.setAppId(appId);
            return appDataCollectMapper.updateByPrimaryKeySelective(appDataCollect) > 0;
        }

        AppDataCollect collect = new AppDataCollect();
        collect.setCreateTime(LocalDateTime.now());
        collect.setAppName(appName);
        collect.setAppPkg(appPkg);
        collect.setTargetUrl(targetUrl);
        collect.setJson(json);
        collect.setAppId(appId);
        collect.setModifyTime(LocalDateTime.now());
        return appDataCollectMapper.insertSelective(collect) > 0;


    }
}
